using System;
using System.Reflection;

namespace Tribe.Cache.Test.Helpers
{
    public class TestHelper
    {
        public static T RunInstanceMethod<T>(Type type, string methodName, object objInstance, object[] @params) where T : class
        {
            const BindingFlags eFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
            return RunMethod<T>(type, methodName, objInstance, @params, eFlags);
        }
        
        public static T RunStaticMethod<T>(Type type, string methodName, object[] @params) where T : class
        {
            const BindingFlags eFlags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
            T t = RunMethod<T>(type, methodName, null, @params, eFlags);

            return t;
        }


        private static T RunMethod<T>(Type type, string methodName, object objInstance, object[] @params, BindingFlags bindingFlags) where T : class
        {
            MethodInfo methodInfo = type.GetMethod(methodName, bindingFlags);
            if (methodInfo == null)
            {
                throw new ArgumentException("There is no method '" + methodName + "' for type '" + type + "'.");
            }

            return methodInfo.Invoke(objInstance, @params) as T;
        }


    }
}